<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 **/

namespace Admin\Controller;
use Vendor\Tree;

class ClassLevelController extends ComController
{

    public function add()
    {
        $d_id = $this->USER['uid'];
        $license = M('license_type')->where(" d_id = '$d_id'")->select();
        $this->assign('license', $license);//导航
        $this->display('form');
    }

    public function index($id = 0, $p = 1)
    {
        $p = intval($p) > 0 ? $p : 1;
        $class = M('class_level');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        //获得当前登录驾校id
        $d_id = $this->USER['uid'];

        $type = isset($_POST['type']) ? $_POST['type'] : '';
        $keyword = isset($_POST['keyword']) ? htmlentities($_POST['keyword']) : '';
        $where = '1 = 1 ';
        if($d_id){
            $where .= "and {$prefix}class_level.d_id = '$d_id' and {$prefix}class_level.d_id > '1'";
        }
        if($type){
            $where .= "and {$prefix}class_level.type = '$type'";
        }
        if ($keyword) {
            $where .= "and {$prefix}class_level.name like '%{$keyword}%' ";
        }

        $d_id = $this->USER['uid'];
        $license = M('license_type')->where(" d_id = '$d_id'")->select();
        $tree = new Tree($license);
        $str = "<option value=\$type \$selected>\$spacer\$type</option>"; //生成的形式
        $license = $tree->get_tree(0, $str);
        $this->assign('license', $license);//导航

        $count = $class->where($where)->count();

        $list = $class->field("{$prefix}class_level.*,{$prefix}member.name as school_name")
            ->where($where)
            ->join("{$prefix}member ON {$prefix}member.uid = {$prefix}class_level.d_id")
            ->order("id desc")
            ->limit($offset . ',' . $pagesize)
            ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('license', $license);//导航
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();

    }

    public function del()
    {
        $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;

        if ($ids) {
            if (is_array($ids)) {
                $aids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }

            if($comments = M("class_level")->where($map)->delete()){
                $this->success('当前班型删除成功！');
            }else{
                $this->error('参数错误，请联系您的技术支持！');
            }
        } else {
            $this->error('您没有选择要删除的班型！');
        }
    }

    public function edit()
    {
        $id = I('get.id');
        $d_id = $this->USER['uid'];
        $var = M('class_level')->where('id=' . $id)->find();
        if ($var) {

            $license = M('license_type')->where(" d_id = '$d_id'")->select();
            $this->assign('license', $license);//导航

            $this->assign('var', $var);
        } else {
            $this->error('参数错误！');
        }
        $this->assign('id',$id);
        $this->display('form');
    }

    public function update()
    {
        $type=I('post.type');
        if(empty($type)){
            $this->error('驾照类型不能为空!');
        }
        $varname = I('post.varname');
        $where['id']=I('post.id');
        $data['d_id'] = $this->USER['uid'];
        $data['name'] = I('post.name');
        $data['type'] = $type;
        $data['price'] = I('post.price');
        $data['notes'] = I('post.notes');

        if ($varname == '') {
            M('class_level')->data($data)->add();
            $this->success('恭喜，操作成功！', U('index'));
        } else {
            M('class_level')->data($data)->where($where)->save();
            $this->success('恭喜，操作成功！', U('index'));
        }

    }

}
